package Application.MapApplication.view.scene
{
	import Application.MapApplication.def.Def;
	import Application.MapApplication.def.Manager;
	import Application.MapApplication.view.ViewBaseMed;
	
	import MapBase.logic.MapBase;
	import MapBase.logic.control.MapBaseControl.Mouse.*;
	import MapBase.logic.control.MapBaseControl.Mouse.MapBaseDragEntity;
	import MapBase.logic.struct.GridCell;
	import MapBase.logic.vo.MapBaseVO;
	
	import com.friendsofed.isometric.IsoObject;
	import com.friendsofed.isometric.IsoUtils;
	import com.friendsofed.isometric.IsoWorld;
	import com.friendsofed.isometric.Point3D;
	import com.friendsofed.isometric.PointExchange;
	import com.utils.keymanager.Key;
	import com.utils.keymanager.KeyCombo;
	import com.utils.keymanager.KeyManager;
	
	import def.XG;
	
	import fl.containers.UILoader;
	import fl.controls.Label;
	
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.net.URLRequest;
	
	import flashx.textLayout.elements.BreakElement;
	
	import manager.XMouseManager;
	
	import mx.graphics.codec.JPEGEncoder;
	
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;
	
	public class MapEditSceneMed extends ViewBaseMed
	{
		static public const NAME : String = "MapEditSceneMed";
		
		private var _mapBase : MapBase;
		
		public var  vo : MapBaseVO;
	
		private var _selectImgObjModel : Object;
		
		private var _labP3 : Label;
		
//		private var _player : Avatar;
		
		public function MapEditSceneMed()
		{
			super("");
			
			vo = MapBase.vo;
			
			var kc:KeyCombo = Manager.KM.createKeyCombo(Key.S);
			kc.addEventListener(KeyCombo.COMBO_PRESSED,KeySDown);
			
			kc = Manager.KM.createKeyCombo(Key.R);
			kc.addEventListener(KeyCombo.COMBO_PRESSED,KeyRDown);
			
			kc = Manager.KM.createKeyCombo(Key.O);
			kc.addEventListener(KeyCombo.COMBO_PRESSED,KeyODown);
			
			kc = Manager.KM.createKeyCombo(Key.B);
			kc.addEventListener(KeyCombo.COMBO_PRESSED,KeyAddAvatar);
			
			_mapBase = new MapBase;
			
			_labP3=new Label;
			
			XG.stageLayer.mouse.addChild(_labP3);
			
//			_player = new Avatar;
//			_player.angleIndex = 2;
//			_player.gender = "F"
//			_player.y = 100;
//			_player.build();
//
//			_player.addEventListener(Event.ENTER_FRAME,run);

			Manager.Mouse.addFun(NAME,showP3,XMouseManager.FUNTYPE_MOUSEMOVE);
			Manager.Mouse.addFun(NAME,_mapBase.update,XMouseManager.FUNTYPE_MOUSEMOVE);
			
			Manager.Mouse.startMove();
			
			_mapBase.srcList = Manager.XML.MapSrc.Data["object"];
			_mapBase.buildFloor = Manager.XML.BuildingFloor.Data;
			 
			_mapBase.world = XG.stageLayer.world;
			_mapBase.Init();

			setWorldPosition();
			
			_mapBase.world.addEventListener(MouseEvent.MOUSE_DOWN,OnWorldMDown);
			_mapBase.world.addEventListener(MouseEvent.MOUSE_UP,OnWorldMUp);
		}
		
		private function run(evt : Event) : void
		{
//			_player.run();
		}
		
		private function showP3(evt : MouseEvent):void
		{
			_labP3.x = XG.stage.mouseX+20;
			_labP3.y = XG.stage.mouseY;
			_labP3.text = _mapBase.getMouseGridCell().toString();
		}
		
		private function KeyRDown(evt:Event):void
		{
			_mapBase.reset();
		}
		
		private function KeyODown(evt:Event) : void
		{
			_mapBase.focusEntity();
		}
		
		private function KeySDown(evt:Event):void
		{
			_mapBase.saveEntityFloor();
		}
		
		private function hit():void
		{
			_mapBase.getMouseClickObj();
		}
		
		private function KeyAddAvatar(evt:Event):void
		{
//			_mapBase.addObjToWorld(_player,"player");
		}
		
		private function OnWorldMDown(evt : MouseEvent) : void
		{
			var wd : IsoWorld = _mapBase.world;
/**************************对象层***************************************/
			MapBaseEditIsoObject.MDownCMD(_mapBase,vo.eIsoObj);
			MapBaseDelEntity.MDownCMD(_mapBase,vo.sleObjLayer,vo.sleDelObj,true);
			MapBaseDragEntity.MDownCMD(_mapBase,vo.sleObjLayer,vo.sleDragEntity,vo.sleDelObj,vo.showEntityFloor);
	
			MapBaseMouseHand.MDownCMD(_mapBase,Manager.Mouse.loadHandBox,_selectImgObjModel,vo.sleObjLayer,vo.sleDelObj,vo.dragFlag);
			MapBaseGridLayer.MDownCMD(_mapBase,vo.sleObjLayer);
/**************************网格成***************************************/				
			MapBaseDrayWorld.MDownCMD(_mapBase,vo.sleGridLayer,vo.dragFlag);
			MapBaseDrawSingleGridTileByMouse.MDownCMD(_mapBase,vo.sleGridLayer);
			MapBasePutDot.MDownCMD(_mapBase,vo.slePutDot);
		}
		
		private function OnWorldMUp(evt : MouseEvent) : void
		{
			MapBaseDrayWorld.MUpCMD(_mapBase,vo.dragFlag);
			MapBaseDragEntity.MUpCMD(_mapBase,vo.sleDragEntity);
		}
		
		override public function listNotificationInterests() : Array
		{
			var notificatons : Array = super.listNotificationInterests();
			
			notificatons=notificatons.concat(  [Def.CreateMap_Show,
												Def.CreateMap_Close,
												Def.CreateMap_Turn,
												Def.STAGE_SIZE_CHANGED,
												Def.CreateMap_CreateGride,
												Def.MapEdit_DragMap,
												Def.MapEdit_ClearPan,
												Def.MapEdit_SelectObjLayer,
												Def.MapEdit_SelectGridLayer,
												Def.MapEdit_SelectBkLayer,
												Def.MapEdit_PutDot,
												Def.MapEdit_SelectImgObjModel,
												Def.MapEdit_DelObj,
												Def.MapEdit_Save,
												Def.MapEdit_OpenMapXML,
												Def.MapEdit_ShowGrid,
												Def.MapEdit_DragEntity,
												Def.MapEdit_GridAlphaChanged,
												Def.MapEdit_WorldLayerAlphaChanged,
												Def.MapEdit_EditIsoObj,
												Def.MapEdit_EIsoObj,
												Def.MapEdit_IsoObjFloor
												]);
			
			return notificatons;
		}
		
		override public function handleNotification(notification:INotification):void
		{
			switch(notification.getName())
			{
				case Def.STAGE_SIZE_CHANGED:
					setWorldPosition();
					break;
				case Def.CreateMap_CreateGride:
					var content : Object = notification.getBody();
					OnCreateMap(content["tileSize"],content["colNum"],content["rowNum"],content["mapName"],content["bkMapName"],content["mapType"]);
					break;
				case Def.MapEdit_DragMap:
					vo.dragFlag = notification.getBody();
					break;
				case Def.MapEdit_ClearPan:
					Manager.Mouse.loadHandBox.unload();
					break;
				case Def.MapEdit_SelectBkLayer:
					vo.sleBkLayer = notification.getBody();
					break;
				case Def.MapEdit_SelectGridLayer:
					vo.sleGridLayer = notification.getBody();
					break;
				case Def.MapEdit_SelectObjLayer:
					vo.sleObjLayer = notification.getBody();
					break;
				case Def.MapEdit_PutDot:
					vo.slePutDot = notification.getBody();
					break;
				case Def.MapEdit_SelectImgObjModel:
					_selectImgObjModel = notification.getBody() as Object;
					break;
				case Def.MapEdit_DelObj:
					vo.sleDelObj = notification.getBody();
					break;
				case Def.MapEdit_Save:
					_mapBase.save();
					break;
				case Def.MapEdit_OpenMapXML:
					_mapBase.open(setWorldPosition);
					break;
				case Def.MapEdit_ShowGrid:
					vo.sleShowGrid = notification.getBody();
					_mapBase.isoGridLayer.visible = vo.sleShowGrid;
					break;
				case Def.MapEdit_DragEntity:
					vo.sleDragEntity = notification.getBody();
					break;
				case Def.MapEdit_GridAlphaChanged:
					_mapBase.isoGridLayer.alpha = Number(notification.getBody())/100;
					break;
				case Def.MapEdit_WorldLayerAlphaChanged:
					_mapBase.world.worldLayer.alpha =  Number(notification.getBody())/100;
					break;
				case Def.MapEdit_EditIsoObj:
					
					break;
				case Def.MapEdit_EIsoObj:
					vo.eIsoObj = notification.getBody();
					break;
				case Def.MapEdit_IsoObjFloor:
					vo.showEntityFloor = notification.getBody();
					if(vo.showEntityFloor)
					{
						_mapBase.showEntityFloor();
					}
					else
					{
						_mapBase.hideEntityFloor();
					}
					break;
				default :
					break;
			}
			
			super.handleNotification(notification);
		}
		
		private function OnCreateMap(tileSize:int,colNum:int,rowNum:int,mapName:String,bkMapName:String,mapType:int) : void
		{
			_mapBase.reset();
			
			vo.mapName = mapName;
			vo.bkMapName = bkMapName;
			vo.mapType = mapType;
			
			setWorldPosition();
			
			_mapBase.drawBackGround();
			_mapBase.createGrid(int(colNum),int(rowNum));
			_mapBase.drawGrid(int(tileSize));
		}
		
		private function setWorldPosition():void
		{
			if(vo.mapType == 2)
			{
				_mapBase.setWorldLocation(0,0);
			}else if(vo.mapType == 1)
			{
				_mapBase.setWorldLocation(XG.stage.stageWidth/2,0);
			}else if(vo.mapType == 3)
			{
				_mapBase.setWorldLocation(0,0);
			}
		}
	}
}